package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.AttendanceDevicesActivity;
import com.lzx.iteam.AttendanceMemberActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.DeviceMsgData;
import com.lzx.iteam.bean.ScheduleManager;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;

/**
 * Created by Megan on 16/3/14.
 */
public class AttendanceDevicesAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<DeviceMsgData> list;
    private int isOpen;

    public AttendanceDevicesAdapter(Context mContext, int isOpen) {
        this.mContext = mContext;
        this.isOpen = isOpen;

    }

    public void bindData(ArrayList<DeviceMsgData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final DeviceMsgData deviceMsgData = list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.attendance_device_list_item, null);
            holder.mTvCurDeviceName = (TextView) convertView.findViewById(R.id.tv_attendance_current_devices_name);
            holder.mTvCurDeviceId = (TextView) convertView.findViewById(R.id.tv_attendance_current_devices_id);
            holder.mTvCurDeviceBind = (TextView) convertView.findViewById(R.id.tv_attendance_current_devices_bind);
            holder.mLlCurDeviceBind = (LinearLayout) convertView.findViewById(R.id.ll_attendance_current_devices_bind);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvCurDeviceName.setText(deviceMsgData.getDeviceName());
        holder.mTvCurDeviceId.setText("设备号:" + deviceMsgData.getDeviceId());
        if (isOpen == 1) {
            holder.mTvCurDeviceBind.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.mTvCurDeviceBind.setTextColor(mContext.getResources().getColor(R.color.attendance_bind_unclick));
        }

        holder.mLlCurDeviceBind.setVisibility(View.GONE);
        holder.mTvCurDeviceBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((AttendanceDevicesActivity) mContext).setMyDeviceMsg(deviceMsgData.getId());
            }
        });


        return convertView;
    }


    private class ViewHolder {
        TextView mTvCurDeviceName;
        TextView mTvCurDeviceId;
        TextView mTvCurDeviceBind;
        LinearLayout mLlCurDeviceBind;
    }

}
